package com.carsys.controller;

import com.carsys.dto.ActionResult;
import com.carsys.entity.DetailedOrder;
import com.carsys.entity.SUser;
import com.carsys.service.OrderService;
import com.carsys.vo.VOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpSession;
import com.google.gson.Gson;

import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class OrderController {
    @Autowired
    private OrderService orderService;

 /**
     * 添加订单
     *
     * @return
     */
    @RequestMapping("addorder")
    @ResponseBody
    public ActionResult addOrder(HttpSession session, Integer uid, Integer cid, Integer getid, Integer backid, String oprice) {
//        获取登陆用户信息
        SUser user = (SUser)session.getAttribute("user");

        if(user == null){
            return new ActionResult(404,"您未登陆",null);
        }else{

            orderService.addOrder(user.getId(),cid, getid, backid, oprice);

            return new ActionResult(200,"预订成功",null);
        }

    }
    /**
     * 根据前台数据查询用户所有的订单
     * @param page 页面
     * @param user 要查询的用户实体
     * @return 返回订单json数据
     */
    @RequestMapping(value = "/carsys/order/all", method = RequestMethod.GET)
    @ResponseBody
    public ActionResult getOrders(int page, SUser user){
//        System.out.println(user);
//        System.out.println(page);
        int id =user.getId();
        List<DetailedOrder> list = orderService.getOrderListByPageAndId(page,id);
//        for (DetailedOrder d:list
//             ) {
//            System.out.println(d);
//        }
        ActionResult result = new ActionResult(200,null,list);

        return result;
    }

    @RequestMapping(value = "/carsys/order/delOrder", method = RequestMethod.DELETE)
    public ActionResult delOrderById(int id) {

        boolean flag = orderService.delOrderById(id);
        if (flag) {
            return new ActionResult(200, "删除订单成功", null);
        }
        return null;
    }
 /**
     * 删除订单
     *
     * @return
     */
    @RequestMapping(value = "/delOrder", method = RequestMethod.DELETE)
    public ActionResult delteOrderById(int id) {
        boolean bl = orderService.delteOrderById(id);
        return new ActionResult(200, "删除成功！", null);
    }

    @RequestMapping(value = "/carsys/order/modify", method = RequestMethod.PUT)
    public ActionResult editOrder(@RequestBody DetailedOrder detailedOrder){
        boolean flag = orderService.editOrder(detailedOrder);
        if(flag){
            return new ActionResult(200,"修改成功",null);
        }
        return null;
    }

    @RequestMapping(value = "/carsys/order/getOrderbyid/{id}", method = RequestMethod.GET)
    public ActionResult getOrderById(@PathVariable("id") int id){
        DetailedOrder detailedOrder = orderService.getOrderById(id);
        System.out.println(detailedOrder);
        return new ActionResult(200,null,new Gson().toJson(detailedOrder));
    }

    /**
     * 获取全部的订单
     *
     * @return
     */
    @RequestMapping(value = "/getOrders", method = RequestMethod.GET)
    public List<VOrder> findAllOrders() {
        List<VOrder> order = orderService.findAllOredrs();
        return order;
    }

}
